package htmodels

import (
	"github.com/astaxie/beego/orm"
	"anfang/models"
)

func HTDeviceMessagesAdd(devicemessages *models.DeviceMessages) error {
	o := orm.NewOrm()
	_, err := o.Insert(devicemessages)
	return err
}

func HTDeviceMessagesGetDetail(devicemessages *models.DeviceMessages) error {
	o := orm.NewOrm()
	return o.Read(devicemessages)
}

func HTDeviceMessagesGetCount(search string, searchFiled string, DeviceType int) (int64, error) {
	o := orm.NewOrm()
	m := models.DeviceMessages{}
	count, err := o.QueryTable(m.TableName()).Filter(m.FN_DeviceType(), DeviceType).Filter(searchFiled + "__icontains", search).Count()
	return count, err
}

func HTDeviceMessagesGetList(devicemessagess *[]models.DeviceMessages, fields []string, DeviceType int, offset int64, limit int, search string, searchFiled string, orderby string) error {
	o := orm.NewOrm()
	m := models.DeviceMessages{}
	_, err := o.QueryTable(m.TableName()).Filter(m.FN_DeviceType(), DeviceType).Filter(searchFiled + "__icontains", search).OrderBy(orderby).Offset(offset).Limit(limit).All(devicemessagess, fields...)
	return err
}

func HTDeviceMessagesDelete(Ids []int64) (int64, error) {
	o := orm.NewOrm()
	m := models.DeviceMessages{}
	count, err := o.QueryTable(m.TableName()).Filter("Id" + "__in", Ids).Delete()
	return count, err
}
func HTDeviceMessagesUpdate(devicemessages *models.DeviceMessages) error {
	o := orm.NewOrm()
	_, err := o.Update(devicemessages)
	return err
}

func HTDeviceMessageGetListById(devicemessagess *[]models.DeviceMessages, fields []string, DeviceId int, MessageId int) error {
	o := orm.NewOrm()
	m := models.DeviceMessages{}
	_, err := o.QueryTable(m.TableName()).Filter(m.FN_DeviceId(), DeviceId).Filter(m.FN_Id()+"__gt",MessageId).OrderBy("-" + m.FN_Id()).Limit(20).All(devicemessagess, fields...)
	return err
}

func HTDeviceMessagesGetListByDeviceId(devicemessagess *[]models.DeviceMessages, DeviceId int, fields []string, offset int64, limit int) error {
	o := orm.NewOrm()
	m := models.DeviceMessages{}
	_, err := o.QueryTable(m.TableName()).Filter(m.FN_DeviceId(), DeviceId).OrderBy("-" + m.FN_Id()).Offset(offset).Limit(limit).All(devicemessagess, fields...)
	return err
}